package com.wingscode.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wingscode.common.exception.RRException;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.Query;
import com.wingscode.common.validator.Assert;
import com.wingscode.modules.app.form.LoginForm;
import com.wingscode.modules.common.dao.AgentDao;
import com.wingscode.modules.common.dao.UserDao;
import com.wingscode.modules.common.entity.AgentEntity;
import com.wingscode.modules.common.entity.UserEntity;
import com.wingscode.modules.common.entity.WxUserEntity;
import com.wingscode.modules.common.service.AgentService;
import com.wingscode.modules.common.service.UserService;

import com.wingscode.modules.common.service.WxUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
	@Autowired
	private UserService userService;
	@Resource
    private AgentDao agentDao;
	@Resource
    private WxUserService wxUserService;

    @Override
    public UserEntity queryByMobile(String mobile) {
        return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("mobile", mobile));
    }

    @Override
    public UserEntity login(LoginForm form) {
        UserEntity user = queryByMobile(form.getMobile());
        Assert.isNull(user, "手机号或密码错误");

//        //密码错误
//        if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
//            throw new RRException("手机号或密码错误");
//        }

        return user;
    }

    @Override
    public UserEntity loginByCode(String openId) {
        UserEntity user = new UserEntity();
        WxUserEntity wxUser = wxUserService.getOne(new QueryWrapper<WxUserEntity>().eq("open_id", openId));
        if (wxUser != null) {
            user = baseMapper.selectById(wxUser.getUserId());
        }

        return user;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String mobile = "", username = "";
        Integer status = null;

        if (params.containsKey("mobile"))
            mobile = (String) params.get("mobile");
        if (params.containsKey("username"))
            username = (String) params.get("username");
        if (params.containsKey("status"))
            status = Integer.parseInt((String) params.get("status"));

        IPage<UserEntity> page = this.baseMapper.getList(
                new Query<UserEntity>().getPage(params), mobile, username, status
        );

        return new PageUtils(page);
    }
    
    @Override
	public boolean setRoleUpdate(Long id) {
		UserEntity userEntity = userService.getById(id);
		userEntity.setRole(99);
		return this.update(userEntity,
				new QueryWrapper<UserEntity>().eq("id", id));
	}
	
	@Override
	public boolean cancelRoleUpdate(Long id) {
		UserEntity userEntity = userService.getById(id);
		userEntity.setRole(0);
		return this.update(userEntity,
				new QueryWrapper<UserEntity>().eq("id", id));
	}

	@Override
    public void deleteById(Integer id) { this.baseMapper.deleteById(id); }

    @Override
    public PageUtils getUserList(Map<String, Object> params) {
        Integer agentId = null;
        if (params.containsKey("agentId"))
            agentId = Integer.parseInt( params.get("agentId").toString());

        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
                        .eq(agentId != null,"agent_id", agentId)
                        .orderByDesc("create_time")
        );

        return new PageUtils(page);
    }

    @Override
    public List<UserEntity> getListByAgent(Integer agentId) { return this.baseMapper.getListByAgent(agentId); }

}
